import axios from "axios";

export default {
  data() {
    return {
      areaOptions: [],
      defaultProps: {
        children: "children",
        label: "name",
        checkStrictly: true,
        value: "id",
        emitPath: false
      },
      areaIds: []
    };
  },
  methods: {
    async getAreaOptions() {
      const {
        data: { errno, message, data }
      } = await axios.get("/api/area/area");
      if (!errno) {
        this.areaOptions = data;
      } else {
        this.$message({ type: "error", message });
      }
    },
    getAllAreaIds(data) {
      const temp = data;
      temp.value && this.areaIds.push(temp.value);
      if (temp.children && temp.children.length) {
        for (let i = 0, len = temp.children.length; i < len; i++) {
          this.getAllAreaIds(temp.children[i]);
        }
      }
    },
    handleChange() {
      this.areaIds = [];
      const checkNode = this.$refs.cascader.getCheckedNodes()[0];
      this.getAllAreaIds(checkNode);
    }
  },
  mounted() {
    this.getAreaOptions();
  }
};
